package com.rmfyzxfw.caseprocess.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rmfyzxfw.caseprocess.boot.result.page.Pagination;
import com.rmfyzxfw.caseprocess.enums.EnableStatusEnum;
import com.rmfyzxfw.caseprocess.mapper.DataFieldMapper;
import com.rmfyzxfw.caseprocess.mapper.DataItemMapper;
import com.rmfyzxfw.caseprocess.mapper.DataNodeItemRelMapper;
import com.rmfyzxfw.caseprocess.model.cmd.DataItemCmd;
import com.rmfyzxfw.caseprocess.model.converter.DataItemConverter;
import com.rmfyzxfw.caseprocess.model.param.DataItemListParam;
import com.rmfyzxfw.caseprocess.model.po.DataField;
import com.rmfyzxfw.caseprocess.model.po.DataItem;
import com.rmfyzxfw.caseprocess.model.po.DataNodeItemRel;
import com.rmfyzxfw.caseprocess.model.vo.DataItemVO;
import com.rmfyzxfw.caseprocess.service.DataItemService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author GYW
 * @description 针对表【t_data_item(数据项表)】的数据库操作Service实现
 * @createDate 2024-08-15 10:39:46
 */
@Service
@RequiredArgsConstructor
public class DataItemServiceImpl extends ServiceImpl<DataItemMapper, DataItem>
        implements DataItemService {

    private final DataItemConverter dataItemConverter;
    private final DataNodeItemRelMapper dataNodeItemRelMapper;
    private final DataFieldMapper dataFieldMapper;

    @Override
    public void addDataItem(DataItemCmd dataItemCmd) {
        DataItem dataItem = dataItemConverter.cmd2do(dataItemCmd);
        dataItem.setDataItemId(IdUtil.simpleUUID());
        dataItem.setStatus(EnableStatusEnum.DISABLE.getCode());
        this.save(dataItem);
    }

    @Override
    public Pagination<DataItemVO> dataItemList(DataItemListParam param) {
        LambdaQueryWrapper<DataItem> dataItemLambdaQueryWrapper = Wrappers.lambdaQuery();
        // 查询指定节点数据项
        if (CollectionUtils.isNotEmpty(param.getDataNodeIds())) {
            Set<String> dataItems = dataNodeItemRelMapper
                    .selectList(Wrappers.<DataNodeItemRel>lambdaQuery().in(DataNodeItemRel::getDataNodeId, param.getDataNodeIds()))
                    .stream()
                    .map(DataNodeItemRel::getDataItemId).collect(Collectors.toSet());
            if(CollectionUtils.isEmpty(dataItems)){
                return Pagination.empty();
            }
            dataItemLambdaQueryWrapper.in( DataItem::getDataItemId, dataItems);
        }

        Page<DataItem> page = new Page<>(param.getPageNum(), param.getPageSize());
        Page<DataItem> dataItemPage = this.page(page, dataItemLambdaQueryWrapper
                .eq(StringUtils.isNotBlank(param.getCourtId()), DataItem::getCourtId, param.getCourtId())
                .eq(Objects.nonNull(param.getStatus()), DataItem::getStatus, param.getStatus())
                .like(StringUtils.isNotBlank(param.getItemName()), DataItem::getItemName, param.getItemName())
                .orderByDesc(DataItem::getId));

        return Pagination.build(dataItemPage, ()->{
            List<DataItemVO> dataItemVOS = dataItemConverter.dos2vos(dataItemPage.getRecords());
            dataItemVOS.forEach(div->{
                List<String> fieldNames = dataFieldMapper.selectList(Wrappers.<DataField>lambdaQuery()
                                .eq(DataField::getDataItemId, div.getDataItemId())
                                .select(DataField::getFieldNameCn))
                        .stream()
                        .map(DataField::getFieldNameCn)
                        .collect(Collectors.toList());
                div.setFieldNames(fieldNames);

            });

            return dataItemVOS;
        });
    }


}




